package com.dangwen.proto.config;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description
 *
 * @author DangWen
 */
@Configuration
public class MqttConfig {
    private static final String MQTT_BROKER = "tcp://47.120.58.197:1883";
    private static final String MQTT_CLIENT_ID = "spring-boot-mqtt-client";

    @Bean
    public MqttConnectOptions mqttConnectOptions() {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);
        return options;
    }

    @Bean
    public MqttClient mqttClient() throws MqttException {
        MqttClient client = new MqttClient(MQTT_BROKER, MQTT_CLIENT_ID, new MemoryPersistence());
        client.connect(mqttConnectOptions());
        client.subscribe("test_topic/sub_topic_b");
        return client;
    }
}
